import { type PaginationProps } from "@pureadmin/table";
import { ElMessage, FormInstance } from "element-plus";
import {
  GetAssetTransferNames,
  AssetTransferGetAllByPagedAndSort,
  AssetTransferCreate,
} from "@/api/stu/SupplyChainFinance"
import { useRoute } from "vue-router";
import { ref, reactive, nextTick, watch, onMounted } from "vue";
import qs from "qs";
export function useEcommercePlatform() {
  const route = useRoute()
  const dataList = ref<any>([]);
  const loading = ref<boolean>(true);
  const DialogVisible = ref<boolean>(false);
  const projectList = ref([]) //可选的L
  const showDetail = ref(false)
  const isDone = ref(false)
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 20,
    currentPage: 1,
    pageSizes: [10, 20, 50, 100],
    background: true
  });
  const columns: TableColumnList = [
    {
      label: "序号",
      type: "index",
      fixed: "left",
      minWidth: 50
    },
    {
      label: "资产证券化项目名称",
      prop: "assetSecuritizationName",
    },
    {
      label: "资产转让方(甲方)",
      prop: "transferor",
    },
    {
      label: "资产受让方(乙方)",
      prop: "transferee",
    },
    {
      label: "状态",
      prop: "isTransfer",
      cellRenderer: ({ row }) => (
        <el-tag type={
          row.isTransfer == true ? "success" : ""}>
          {
            row.isTransfer == true ? "已接收" : "未接收"
          }
        </el-tag>
      )
    },
    {
      label: "操作",
      prop: "operation",
      width: 180,
      slot: "operation"
    },
  ];
  const formData = reactive({
    assetSecuritizationName: '',
    assetPoolId: '',
    spvName: '',
    supplyName: '',
    assetValue: '',
    targetAsset: '',
    spvId: '',
  })
  // 万亿 长度 14

  async function getList() {
    const res = await AssetTransferGetAllByPagedAndSort({
      pageIndex: pagination.currentPage,
      pageCount: pagination.pageSize,
      planId: route.query.planId,
      taskId: route.query.taskId,
    });
    if (res.success) {
      dataList.value = res.data.items;
      loading.value = false;
      pagination.total = res.data.totalCount;
    } else {
      ElMessage.error(res.msg);
      setTimeout(() => { loading.value = false; }, 1500)
    }
  }

  function changePro(item) {
    formData.spvId = item.spvId
    formData.supplyName = item.supplyName
    formData.spvName = item.spvName
    formData.assetValue = item.smallLoanCreditbalance
    formData.targetAsset = item.assetSecuritizationName + '项目资产池全部资金'
    formData.assetSecuritizationName = item.assetSecuritizationName
  }
  async function SubtimEntryPlatform() {
    if (formData.assetPoolId == '') {
      ElMessage.warning('选择资产证券化项目')
      return
    }
    const data = {
      planId: route.query.planId,
      taskId: route.query.taskId,
      spvId: formData.spvId,
      assetPoolId: formData.assetPoolId,
      transferee: formData.spvName,
      transferor: formData.supplyName,
      assetValue: formData.assetValue,
      targetAsset: formData.targetAsset,
      assetSecuritizationName: formData.assetSecuritizationName,
    };
    const res = await AssetTransferCreate(data);
    if (res.success) {
      ElMessage.success(res.msg)
      CloseEntryPlatform();
      getList()
    } else {
      ElMessage.error(res.msg)
    }
  }

  function CloseEntryPlatform() {
    formData.assetPoolId = ''
    formData.spvName = ''
    formData.assetValue = ''
    formData.supplyName = ''
    formData.targetAsset = ''
    DialogVisible.value = false
    showDetail.value = false
    isDone.value = false
  }

  function checkDetail(item) {
    showDetail.value = true;
    DialogVisible.value = true;
    if (item.isTransfer == true) {
      isDone.value = true
    }
    formData.assetPoolId = item.assetPoolId
    formData.supplyName = item.transferor
    formData.spvName = item.transferee
    formData.assetValue = item.assetValue
    formData.targetAsset = item.targetAsset
  }

  onMounted(async () => {
    const data = {
      taskId: route.query.taskId,
      planId: route.query.planId,
    }
    const res = await GetAssetTransferNames(data);
    projectList.value = res
    getList()
  })

  function handleSizeChange() {
    getList()
  }
  function handleCurrentChange() {
    getList()
  }

  return {
    columns,
    loading,
    dataList,
    pagination,
    handleSizeChange,
    handleCurrentChange,
    DialogVisible,
    formData,
    projectList,
    changePro,
    CloseEntryPlatform,
    SubtimEntryPlatform,
    checkDetail,
    isDone,
    showDetail,
  }
}
